package com.course.system.controller;

import com.course.server.common.PageRequest;
import com.course.server.common.Result;
import com.course.server.dto.ResourceDto;
import com.course.server.service.ResourceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author: Durian
 * @date: 2021/6/6 12:27
 */
@Validated
@RestController
@RequestMapping("/admin/resources")
public class ResourceController {

    /**
     * 用于日志记录使用
     */
    public static final String BUSINESS_NAME = "资源管理";

    @Resource
    private ResourceService resourceService;

    @GetMapping
    public Result list(@Validated PageRequest request) {
        return Result.success(resourceService.list(request.getPage(), request.getSize()));
    }

    @PostMapping
    public Result save(@RequestBody @Validated ResourceDto resourceDto) {
        resourceService.save(resourceDto);
        return Result.success();
    }

    @DeleteMapping("{resourceId}")
    public Result delete(@PathVariable("resourceId") @NotBlank(message = "待删除资源ID不能为空") String resourceId) {
        resourceService.delete(resourceId);
        return Result.success();
    }

    @GetMapping("/loadTree")
    public Result getResourceTree() {
        return Result.success(resourceService.getResourceTree());
    }
}
